package com.ggh.jinjilive.ui.social.near;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.amap.api.services.core.LatLonPoint;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.social.near.NearLive;
import com.ggh.jinjilive.mobile.social.near.NearUser;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class NearViewModel extends BaseViewModel {
    //经纬度
    public ObservableField<LatLonPoint> latLonPointObservableField = new ObservableField<>();
    //区分
    public ObservableField<String> stringObservableField = new ObservableField<>("广场");
    //男女
    public ObservableField<String> gender = new ObservableField<>("男");
    //男列表
    public ObservableField<List<Integer>> genderIntegerList = new ObservableField<>();
    //女列表
    public ObservableField<List<Integer>> femaleIntegerList = new ObservableField<>();
    //附近用户列表
    public MutableLiveData<List<NearUser>> listMutableLiveData = new MutableLiveData<>();
    //附近直播列表
    public MutableLiveData<List<NearLive>> nearData = new MutableLiveData<>();

    public NearViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }

    /**
     * 附近用户
     */
    public void nearbyUser(){
        if (isRefresh) {
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> paramsa = new HashMap<>();
        if(stringObservableField.get().equals("筛选")){
            if(gender.get().equals("男")){
                paramsa.put("sex", 1);
                paramsa.put("info_type",genderIntegerList.get());
            }else if(gender.get().equals("女")){
                paramsa.put("sex", 2);
                paramsa.put("info_type",femaleIntegerList.get());
            }
        }
        paramsa.put("page",page);
        paramsa.put("limit",pageSize);
        paramsa.put("lat", latLonPointObservableField.get().getLatitude());
        paramsa.put("lng", latLonPointObservableField.get().getLongitude());
        addSubscribe(apiCallInterface.nearbyUser(paramsa).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<NearUser>>>() {
                    @Override
                    public void onNextData(ResultEntity resultEntity) {
                        List<NearUser> nearUserList = (List<NearUser>) resultEntity.getData();
                        listMutableLiveData.setValue(nearUserList);
                    }

                    @Override
                    public void onErrorNet(Throwable t, String netMsg) {

                    }
                }));
    }

    /**
     * 附近直播列表
     */
    public void nearbyLive(){
        if (isRefresh) {
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("page",page);
        paramsa.put("limit",pageSize);
        addSubscribe(apiCallInterface.nearbyLive(paramsa).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<NearLive>>>() {
                    @Override
                    public void onNextData(ResultEntity resultEntity) {
                        List<NearLive> nearLiveList = (List<NearLive>) resultEntity.getData();
                        nearData.setValue(nearLiveList);
                    }

                    @Override
                    public void onErrorNet(Throwable t, String netMsg) {

                    }
                }));
    }
}
